<template>
  <div>
    <Main type="form">
      <el-form
        ref="form"
        label-width="180px"
        label-position="right"
        :model="form"
      >
        <Title :title="'维权原因'" />

        <el-form-item label-width="60px">
          <el-switch
            v-model="open"
            :active-text="open == false ? '已关闭' : '已打开'"
          >
          </el-switch>
          &nbsp;&nbsp;&nbsp;
          说明:设置后买家可以在申请维权时可以选择，最多支持10个
          <div style="display: flex;width: 100%;">
            <div v-if="open">
              <div class="activist-open">
                <div v-for="(item, index) in btnCode" :key="index" class="box">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    :content="item"
                    placement="top"
                  >
                    <div class="box-name">
                      {{ item }}
                    </div>
                  </el-tooltip>
                  <div class="box-del">
                    <i class="el-icon-delete" @click="btnDel(index)"></i>
                  </div>
                </div>
                <div v-if="btnCode.length < 10">
                  <el-form
                    ref="form"
                    :model="form"
                    class="demo-ruleForm"
                    style="display: flex;"
                  >
                    <el-form-item
                      label-width="500"
                      prop="name"
                      :rules="{
                        required: true,
                        message: '相关标签不能为空',
                        trigger: 'blur'
                      }"
                    >
                      <el-input
                        v-model="form.name"
                        placeholder="请输入相关标签"
                        prop="name"
                        size="small"
                        style="width:250px;"
                        maxlength="15"
                        show-word-limit
                      ></el-input>
                    </el-form-item>
                    <el-form-item style="margin-left:10px;">
                      <el-button
                        size="small"
                        type="primary"
                        @click="submitForm('form')"
                        >新增</el-button
                      >
                    </el-form-item>
                  </el-form>
                </div>
              </div>
            </div>
          </div>
        </el-form-item>
        <Title :title="'维权流程设置'" />
        <el-form-item
          label="买家发起退款申请"
          prop="num1"
          :rules="{
            required: true,
            message: '请输入天数',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.num1"
            size="mini"
            :min="0"
            :precision="0"
            :controls="false"
          ></el-input-number
          >&nbsp;&nbsp; 天后商家未处理，系统将自动同意/拒绝退款
        </el-form-item>
        <el-form-item
          label="买家发起退货退款申请"
          prop="num2"
          :rules="{
            required: true,
            message: '请输入天数',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.num2"
            size="mini"
            :min="0"
            :precision="0"
            :controls="false"
          ></el-input-number
          >&nbsp;&nbsp; 天后商家未处理，系统将自动同意维权
        </el-form-item>
        <el-form-item
          label="商家同意退货"
          prop="num3"
          :rules="{
            required: true,
            message: '请输入天数',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.num3"
            size="mini"
            :min="0"
            :precision="0"
            :controls="false"
          ></el-input-number>
          &nbsp;&nbsp; 天后买家未处理，系统将自动拒绝维权
        </el-form-item>
        <el-form-item
          label="买家已退货"
          prop="num4"
          :rules="{
            required: true,
            message: '请输入天数',
            trigger: 'blur'
          }"
        >
          <el-input-number
            v-model="form.num4"
            size="mini"
            :min="0"
            :max="90"
            :precision="0"
            :controls="false"
          ></el-input-number>
          &nbsp;&nbsp; 天后商家未处理，系统将自动同意/拒绝退款
        </el-form-item>

        <el-form-item label="说明:" class="msgs">
          <div>
            <span>1、系统默认时间为7天，手动设置时间最长不超过90天</span>
          </div>
          <div>
            <span
              >2、县长支付的订单系统自动做同意处理，线下支付的订单系统自动拒绝处理</span
            >
          </div>
        </el-form-item>
      </el-form>
    </Main>
    <Footer>
      <el-button type="primary" @click="submit">保 存</el-button>
    </Footer>
  </div>
</template>
<script>
import { ACTIVISTSET } from "../../service";
export default {
  data() {
    return {
      open: false,
      btnCode: [],
      is_system_reason: "",
      form: {
        num1: 0,
        num2: 0,
        num3: 0,
        num4: 0
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    btnDel(i) {
      this.btnCode.splice(i, 1);
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.btnCode.push(this.form.name);
          this.form.name = "";
        } else {
          return false;
        }
      });
    },
    async getList() {
      await ACTIVISTSET().then(({ code, data }) => {
        if (code === 200) {
          this.open = data.is_system_reason === 1;
          this.btnCode = data.system_reason.split(",");
          this.form.num1 = data.refund_money;
          this.form.num2 = data.refund_money_goods;
          this.form.num3 = data.agree_refund;
          this.form.num4 = data.already_refund_goods;
        }
      });
    },
    async submit() {
      await ACTIVISTSET({
        flag: "save",
        is_system_reason: this.open ? 1 : 0,
        system_reason: this.btnCode.join(","),
        refund_money: this.form.num1,
        refund_money_goods: this.form.num2,
        agree_refund: this.form.num3,
        already_refund_goods: this.form.num4
      }).then(res => {
        if (res.code === 200) {
          this.$message({
            message: "设置成功",
            type: "success"
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.activist-open {
  display: flex;
  flex-wrap: wrap;
  margin-right: 10px;
  .box {
    width: 200px;
    height: 40px;
    line-height: 40px;
    background: #ebecf0;
    margin: 0 10px 10px 0;
    font-size: 14px;
    text-align: center;
    color: #666666;
    .box-name {
      float: left;
      width: 88%;
      padding-left: 10px;
      box-sizing: border-box;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .box-del {
      float: left;
      width: 12%;
    }
  }
}
</style>
<style>
.msgs {
  line-height: 20px;
  color: red;
}
.msgs > .el-form-item__label {
  color: red;
}
</style>
